package com.mantru.iotpot.Cook.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.mantru.iotpot.DateClass.StepDiagramDate;
import com.mantru.iotpot.R;

import java.util.List;

public class StepDiagramRecyclerViewAdapter extends RecyclerView.Adapter<StepDiagramRecyclerViewAdapter.ViewHolder> {
    private List<StepDiagramDate> stepDiagramDates;

    public StepDiagramRecyclerViewAdapter(List<StepDiagramDate> stepDiagramDates) {
        this.stepDiagramDates = stepDiagramDates;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_step_diagram_recyclerview, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        StepDiagramDate stepDiagramDate = stepDiagramDates.get(position);
        holder.step_name.setText("步骤" + stepDiagramDate.getSteepIndex() + " :" + stepDiagramDate.getSteepName());
        holder.step_time.setText(String.valueOf(stepDiagramDate.getTotalTime()) + "s");

        // 初始化内层 RecyclerView 适配器
        StepDiagramRecyclerViewAdapterInner innerAdapter = new StepDiagramRecyclerViewAdapterInner(
                holder.recyclerView.getContext(),
                stepDiagramDate.getData()
        );
        holder.recyclerView.setAdapter(innerAdapter);

        // 使用 FlexboxLayoutManager 实现流式布局
        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(holder.recyclerView.getContext());
        layoutManager.setFlexDirection(FlexDirection.ROW);    // 水平方向排列
        layoutManager.setFlexWrap(FlexWrap.WRAP);             // 自动换行
        layoutManager.setJustifyContent(JustifyContent.FLEX_START); // 左对齐

        holder.recyclerView.setLayoutManager(layoutManager);
    }

    @Override
    public int getItemCount() {
        return stepDiagramDates.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView step_time;
        private final TextView step_name;
        private final RecyclerView recyclerView;

        public ViewHolder(View view) {
            super(view);
            step_time = view.findViewById(R.id.adapter_step_diagram_recyclerview_time);
            step_name = view.findViewById(R.id.adapter_step_diagram_recyclerview_name);
            recyclerView = view.findViewById(R.id.adapter_step_diagram_recyclerview_recyclerview);
        }
    }
}